/**
 * 
 */
package de.cbf.cam.dsa.facet.sf;

import java.util.ArrayList;
import java.util.List;

import de.cbf.cam.dsa.facet.sf.constants.ESfType;
import de.cbf.cam.dsa.facet.sf.helper.PrerequisiteSfReference;
import de.cbf.util.entity.facet.constants.ConstantsFontColorLogView;
import de.cbf.util.entity.facet.entities.FacetOfEntity;
import de.cbf.util.entity.facet.util.TextPartStyled;
import de.cbf.util.uuidheader.entities.UUIDHeader;
import de.cbf.util.uuidheader.exceptions.EntityMergeException;

/**
 * @author Alti
 *
 */
public class FacetSf extends FacetOfEntity implements FacetSfConstants{	
	
	
	protected List<PrerequisiteSfReference> fPrerequisiteSfs; //FIXME delete will need to use a Condition
	protected ESfType fSfType;
	
	/**	 */
	public FacetSf() {		
	}

	/**
	 * @param name
	 */
	public FacetSf(String name) {
		super(name);		
	}
	
	/* (non-Javadoc)
	 * @see de.cbf.util.uuidheader.entities.NamedEntity#init()
	 */
	@Override
	protected void init() {		
		super.init();
		fPrerequisiteSfs = new ArrayList<PrerequisiteSfReference>();
		fSfType = UNSET_SfType;
	}

	/* (non-Javadoc)
	 * @see de.cbf.util.entity.facet.interfaces.IFacetOfEntity#getFacetStyledDescription()
	 */
	@Override
	public List<TextPartStyled> getFacetStyledDescription() {
		List<TextPartStyled> textStyleInfo = new ArrayList<TextPartStyled>();		
		TextPartStyled textPart;
		//fSfType
		textPart = new TextPartStyled();
		textPart.setText("\t"+TEXT_LOG_SfType);
		textPart.setStyleConstant(ConstantsFontColorLogView.STYLE_CATEGORY);		
		textStyleInfo.add(textPart);
		//fSfType value
		textPart = new TextPartStyled();
		textPart.setText(fSfType+" \n");
		textPart.setStyleConstant(ConstantsFontColorLogView.STYLE_VALUE);		
		textStyleInfo.add(textPart );
		
		//fPrerequisiteSfs
		textPart = new TextPartStyled();
		textPart.setText("\t"+TEXT_LOG_PrerequisiteSfs);
		textPart.setStyleConstant(ConstantsFontColorLogView.STYLE_CATEGORY);		
		textStyleInfo.add(textPart);
		//fPrerequisiteSfs value
		textPart = new TextPartStyled();
		textPart.setText(fPrerequisiteSfs+" \n");
		textPart.setStyleConstant(ConstantsFontColorLogView.STYLE_VALUE);		
		textStyleInfo.add(textPart );
		return textStyleInfo;
	}

	/* (non-Javadoc)
	 * @see de.cbf.util.entity.facet.interfaces.IFacetOfEntity#hasUnsetValues()
	 */
	@Override
	public String hasUnsetValues() {
		StringBuffer result = new StringBuffer();
		if(UNSET_SfType.equals(fSfType)){
			result.append(TEXT_UNSET_SfType+"\n");
		}
		if(new ArrayList<PrerequisiteSfReference>().equals(fPrerequisiteSfs)){
			result.append(TEXT_UNSET_PrerequisiteSfs+"\n");
		}
		return result.toString();
	}

	/* (non-Javadoc)
	 * @see de.cbf.util.entity.facet.entities.FacetOfEntity#doMergeDataFrom(de.cbf.util.uuidheader.entities.UUIDHeader, boolean)
	 */
	@Override
	protected void doMergeDataFrom(UUIDHeader entity, boolean i_am_older)
			throws EntityMergeException {
		super.doMergeDataFrom(entity, i_am_older);
		if (entity instanceof FacetSf) {
			FacetSf other = (FacetSf) entity;
			if(i_am_older){
				this.fSfType = other.fSfType;
				this.fPrerequisiteSfs.clear();
				this.fPrerequisiteSfs.addAll(other.fPrerequisiteSfs);
			}
		}else{
			String mergeEntityClass = entity == null? "null":entity.getClass().getName();
			String errorMsg ="EntityToMerge "+mergeEntityClass+" does not fit class:"+this.getClass().getName();
			throw new EntityMergeException(errorMsg);
		}
	}

	public ESfType getSfType(){
		return fSfType;
	}

	public void setSfType(ESfType newSfType) {
		if(newSfType == null || newSfType.equals(fSfType)) return;
		ESfType oldSfType = this.fSfType;
		this.fSfType = newSfType;
		propertyChangeSupport.fireIndexedPropertyChange(PN_SfType, PI_SfType,
				oldSfType,this.fSfType);	
	}	

	public List<PrerequisiteSfReference> getPrerequisiteSfs(){
		return new ArrayList<PrerequisiteSfReference>(fPrerequisiteSfs);
	}
	
	public boolean addPrerequisiteSf(PrerequisiteSfReference prerequisiteSf){
		boolean result = fPrerequisiteSfs.add(prerequisiteSf);
		propertyChangeSupport.fireIndexedPropertyChange(PN_PrerequisiteSfs, PI_PrerequisiteSf,
				null,this.fPrerequisiteSfs);
		return result;
	}
	
	public boolean removePrerequisiteSf(PrerequisiteSfReference prerequisiteSf){
		boolean result = fPrerequisiteSfs.remove(prerequisiteSf);
		propertyChangeSupport.fireIndexedPropertyChange(PN_PrerequisiteSfs, PI_PrerequisiteSf,
				null,this.fPrerequisiteSfs);
		return result;
	}

	public void setPrerequisiteSfs(List<PrerequisiteSfReference> newPrerequisiteSfs) {
		if(newPrerequisiteSfs == null || newPrerequisiteSfs.equals(fPrerequisiteSfs)) return;
		this.fPrerequisiteSfs.clear();
		this.fPrerequisiteSfs.addAll(newPrerequisiteSfs);
		propertyChangeSupport.fireIndexedPropertyChange(PN_PrerequisiteSfs, PI_PrerequisiteSf,
				null,this.fPrerequisiteSfs);	
	}
}
